from rest_framework.response import Response


class Code:
    """
    返回数据状态
    """
    Ok = 0
    BadParams = 1000
    UnAuth = 1001
    CSRF = 1003
    NotFound = 1004
    MethodError = 1005
    ServerError = 2000


class APIResponse(Response):
    def __init__(self, code=Code.Ok, msg='', data=None, status=None, template_name=None,
                 headers=None, exception=False, content_type=None,
                 **kwargs):
        res_data = {
            'code': code,
            'msg': msg,
            "data": data,
        }
        # data的响应数据体
        # results可能是False、0等数据，这些数据某些情况下也会作为合法数据返回
        # if results is not None:
        #     data['results'] = results

        res_data.update(kwargs)

        # 重写父类的Response的__init__方法
        super().__init__(data=res_data, status=status, headers=headers, exception=exception,
                         template_name=template_name,
                         content_type=content_type)
